<?php
// +----------------------------------------------------------------------
//            -------------------------
//           /   / ----------------\  \
//          /   /             \  \
//         /   /              /  /
//        /   /    /-------------- /  /
//       /   /    /-------------------\  \
//      /   /                   \  \
//     /   /                     \  \
//    /   /                      /  /
//   /   /      /----------------------- /  /
//  /-----/      /---------------------------/
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://baimifan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: Sage 2016/4/22  16:46
// +----------------------------------------------------------------------
namespace Admin\Controller;
class CardExchangeController extends CashierBaseController {

    function index() {
        $this->display();
    }

    //挂失冻结
    function doExchange() {
        $card_id = I("post.card_id");
        $newcard_id = I("post.newcard_id");
        if (substr($newcard_id,0,2) == 16) {
            $j = ["status" => "error" , "data" => "新卡不能为16开头"];
            echo json_encode($j);
            return;
        }
        $Card = M("Card");
        $d = $Card->where(['card_id' => $card_id])->find();
        $newd = $Card->where(['card_id' => $newcard_id])->find();
        if (!$d || !$newd) {
            $j = ["status" => "error" , "data" => "会员卡不存在"];
            echo json_encode($j);
            return;
        }
        if($d['user_id'] == "" || $d['user_id'] == null ){
            $j = ["status" => "error" , "data" => "原卡已更换过，没有用户绑定"];
            echo json_encode($j);
            return;
        }
        unset($d['card_id']);
        $Card->startTrans();
        //旧卡不变，user_Id置空
        $data_d=$d;
        $data_d['user_id']=null;
        $data_d['remark']=$data_d['remark']."于".date("Y-m-d h:i:s")."换卡至卡号：".$newcard_id.";<br>";
        $r=$Card->where(["card_id"=>$card_id])->data($data_d)->save();
        if(!$r){
            $Card->rollback();
            $j = ["status" => "error" , "data" => "系统错误，无法更换"];
            echo json_encode($j);
            return;
        }
        $data_newd=$d;
        $data_newd['remark']=$newd['remark']."于".date("Y-m-d h:i:s")."换卡,原卡号：".$card_id.";<br>";
        $r=$Card->where(["card_id"=>$newcard_id])->data($data_newd)->save();
        if(!$r){
            $Card->rollback();
            $j = ["status" => "error" , "data" => "系统错误，无法更换"];
            echo json_encode($j);
            return;
        }
        $Card->commit();
        $j = ["status" => "success" , "data" => "换卡成功"];
        echo json_encode($j);
        return;
    }

    function cardShow() {
        $card_id = I("get.card_id");
        $d = M("Card")->where(["card_id" => $card_id])->find();
        if ($d) {
            $d['child_sex'] = $d['child_sex'] == 1 ? "男" : "女";
            $d['is_lock'] = $d['is_lock'] == -1 ? "冻结" : "非冻结";
        }
        $this->assign("d" , $d);
        $this->display("CardHandle:cardShow");
    }

    public function bind()
    {
        $this->display();
    }

    public function bindExchange()
    {
        $card_id = I("post.card_id");
        $newcard_id = I("post.newcard_id");
        $Card = M("Card");
        $d = $Card->where(['card_id' => $card_id])->find();
        $newd = $Card->where(['card_id' => $newcard_id])->find();
        if (!$d || !$newd) {
            $this->ajaxReturn(["status" => "error" , "data" => "会员卡不存在"]);
        }
        $userModel = M('User');
        $openid = $userModel->where(['user_id' => $newd['user_id']])->getField('openid');
        if (!$openid) {
            $this->ajaxReturn(["status" => "error" , "data" => "新卡没有在微信注册过"]);
        }
        // 把16的openid 移动到 15上
        $Card->startTrans();
        $openid = $userModel->where(['user_id' => $newd['user_id']])->getField('openid');
        $newUser = $userModel->where(['user_id' => $d['user_id']])->data(['openid' => $openid])->save();
        if(!$newUser){
            $Card->rollback();
            $j = ["status" => "error" , "data" => "系统错误，无法更换"];
            echo json_encode($j);
            return;
        }
        $Card->where(['card_id' => $card_id])->data(['remark' => date("Y-m-d h:i:s").'更改为user_id'.$newd['user_id'].'的openid'])->save();
        $oldUser = $userModel->where(['user_id' => $newd['user_id']])->data(['openid' => null])->save();
        if(!$oldUser){
            $Card->rollback();
            $j = ["status" => "error" , "data" => "系统错误，无法更换"];
            echo json_encode($j);
            return;
        }
        $Card->where(['card_id' => $newcard_id])->data(['remark' => date("Y-m-d h:i:s").'openid 移动给user_id'.$d['user_id']])->save();
        $userModel->commit();
        $j = ["status" => "success" , "data" => "换卡成功"];
        echo json_encode($j);
        return;
    }
}